/**
 * Created by meigang on 17/9/9.
 */
// Usage
//
//   require.register("browser/debug.js", function(module, exports, require){
//     // Module code goes here
//   });
//
//   var debug = require("browser/debug.js");

function require(p){
    var path = require.resolve(p);
    var mod = require.modules[path];
    if (!mod) {// if not exists,load this by ajax
        var js = ajax(require.config.basePath+p);
        if(js){
            eval(js);
        }else{
            throw new Error('failed to require "' + p + '"');
        }
    }
    //再次获取
    mod = require.modules[path];
    if (!mod.exports) {
        mod.exports = {};
        mod.call(mod.exports, mod, mod.exports, require.relative(path));
    }
    return mod.exports;

}

require.config = {
    basePath:"/"
}

require.modules = {};

require.resolve = function (path){
    var orig = path;
    var reg = path + '.js';
    var index = path + '/index.js';
    return require.modules[reg] && reg
        || require.modules[index] && index
        || orig;
};

require.register = function (path, fn){
    require.modules[path] = fn;
};

require.relative = function (parent) {
    return function(p){
        if ('.' != p.charAt(0)) return require(p);
        var path = parent.split('/');
        var segs = p.split('/');
        path.pop();

        for (var i = 0; i < segs.length; i++) {
            var seg = segs[i];
            if ('..' == seg) path.pop();
            else if ('.' != seg) path.push(seg);
        }

        return require(path.join('/'));
    };
};
/**
 * 自定义ajax实现
 * @describe 为了保证此类独立性
 * @url 请求的资源地址
 * @callback 回调函数，如果存在此函数则采用异步调用
 */
var ajax = function(url,callback){
    var xmlHttp = getXMLHttpRequest();
    var re = "";
    //是否为异步操作
    var async = (callback ==null ? false : true);
    xmlHttp.open("get",url,async);
    xmlHttp.onreadystatechange = function(){
        if(xmlHttp.readyState == 4){//数据返回成功
            if (xmlHttp.status == 200) {
                re = xmlHttp.responseText;
                if(callback != null){
                    callback(re);
                }
            }
            else{
                if(xmlHttp.status == 0){
                    console.log("请求["+url+"]超时");
                }else{
                    console.log("请求["+url+"]失败");
                }
            }
        }
    }
    xmlHttp.send(null);
    return re;
};
//获取XMLHttpRequest
function getXMLHttpRequest(){
    xmlhttp = null;
    if (window.XMLHttpRequest) {// code for all new browsers
        xmlhttp = new XMLHttpRequest();
    }else if (window.ActiveXObject) {// code for IE5 and IE6
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlhttp;
}

function exec(script){
    if(window.execScript) {
        // 给IE的特殊待遇
        window.execScript(script);
    } else {
        // 给其他大部分浏览器用的
        window.eval(script);
    }
}